package cc.mrbird.febs.business.controller;

import cc.mrbird.febs.business.entity.Business;
import cc.mrbird.febs.business.service.IBusinessService;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Slf4j
@Validated
@Controller
@RequestMapping(value = "/business")
public class BusinessController extends BaseController {

    @Resource
    private IBusinessService businessService;

    @PostMapping("/add")
    @ResponseBody
    public FebsResponse add(Business business) {
        if (business.getName().trim().equals("")) {
            return new FebsResponse().fail().message("业务名不能为空!");
        }
        if (businessService.add(business)) {
            return new FebsResponse().success();
        } else {
            return new FebsResponse().fail().message("该业务名已存在!");
        }
    }

    @PostMapping("/update")
    @ResponseBody
    public FebsResponse update(Business business) {
        if (business.getName().trim().equals("")) {
            return new FebsResponse().fail().message("业务名不能为空!");
        }
        if (businessService.update(business)) {
            return new FebsResponse().success();
        } else {
            return new FebsResponse().fail().message("该业务名已存在!");
        }
    }

    @PostMapping("/remove")
    @ResponseBody
    public FebsResponse remove(Business business) {
        if (businessService.remove(business)) {
            return new FebsResponse().success();
        } else {
            return new FebsResponse().fail().message("该业务已被使用，无法删除!");
        }
    }

    @PostMapping("/listTreeData")
    @ResponseBody
    public FebsResponse listTreeData(@RequestParam("type") String type) {
        return new FebsResponse().success().data(businessService.listTreeData(type));
    }


    @PostMapping("/treeData")
    @ResponseBody
    public FebsResponse treeData(String type, String name) {
        return new FebsResponse().success().data(businessService.treeData(type, name));
    }
}
